﻿using Shop.Model.DB;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Shop.Repository.lmpl
{
    internal class AttrTemplateRepository : ShopBaseRepository<DBGoodsAttrTemplate, long>, IAttrTemplateRepository
    {
        public AttrTemplateRepository (ISqlClientFactory factory) : base(factory)
        {
        }
        public void SetSort (Dictionary<long, int> sorts)
        {
            if (!base.Update(sorts.ConvertAll(a => new DBGoodsAttrTemplate
            {
                Id = a.Key,
                Sort = a.Value
            }), "Sort"))
            {
                throw new ErrorException("shop.attr.template.set.fail");
            }
        }
        public long Add (DBGoodsAttrTemplate add)
        {
            add.Id = IdentityHelper.CreateId();
            add.IsEnable = true;
            base.Insert(add);
            return add.Id;
        }
        public void SetIsEnable (long id, bool isEnable)
        {
            if (!base.Update(a => a.IsEnable == isEnable, a => a.Id == id))
            {
                throw new ErrorException("shop.attr.set.fail");
            }
        }
        public void SetIsEnable (long[] ids, bool isEnable)
        {
            if (!base.Update(a => a.IsEnable == isEnable, a => ids.Contains(a.Id)))
            {
                throw new ErrorException("shop.attr.set.fail");
            }
        }
    }
}
